﻿#include "MusicList.h"
#include"MusicListDelegate.h"
#include"MusicListView.h"
#include<qmutex.h>
#include<qboxlayout.h>
#include<ctime>

MusicList::MusicList(QWidget*parent)
	: QWidget(parent),
	m_mutex(new QMutex),
	m_delegate(new MusicListDelegate(this)),
	m_listView(new MusicListView(this)),
	m_playIndex(0),
	m_playMode(PlayMode::PlayInOrder)
{
	/*m_listView->setModel(m_model);*/
	/*m_models[ListType::PlayList] = new QStandardItemModel;
	m_models[ListType::RectentList] = new QStandardItemModel;
	m_models[ListType::SearchList] = new QStandardItemModel;*/

	m_curModel = new QStandardItemModel;
	m_listView->setItemDelegate(m_delegate);
	m_listView->setModel(m_curModel);
	m_listView->setEditTriggers(QAbstractItemView::NoEditTriggers);

	//m_listType = ListType::PlayList;
	//m_curModel = m_models[m_listType];

	QVBoxLayout* layOut = new QVBoxLayout(this);
	layOut->addWidget(m_listView);

	

	initAllConnect();

#ifdef QT_DEBUG
	//QStandardItem* item = new QStandardItem("123_Test      456");
	//m_curModel->appendRow(item);
#endif // QT_DEBUG

}

MusicList::~MusicList()
{
	delete m_mutex;
}

void MusicList::appendSong(const Song & song)
{
	QMutexLocker<QMutex> locker(m_mutex);
	QStandardItem* item = new QStandardItem;
	item->setData(song.cover, Qt::UserRole + 1);
	item->setData(song.songName, Qt::UserRole + 2);
	item->setData(song.singerName, Qt::UserRole + 3);
	item->setData(song.duration_s, Qt::UserRole + 4);
	item->setData(song.url, Qt::UserRole + 5);
	item->setData(false, Qt::UserRole + 6);	//is Playing row
	emit appendSongFinished(item);
}

void MusicList::appendSong(const QList<Song>& list)
{
	QMutexLocker<QMutex> locker(m_mutex);

	for (auto& i : list)
	{
		QStandardItem* item = new QStandardItem;
		item->setData(i.cover, Qt::UserRole + 1);
		item->setData(i.songName, Qt::UserRole + 2);
		item->setData(i.singerName, Qt::UserRole + 3);
		item->setData(i.duration_s, Qt::UserRole + 4);
		item->setData(i.url, Qt::UserRole + 5);
		item->setData(false, Qt::UserRole + 6);
		emit appendSongFinished(item);
	}

}

void MusicList::push_back(const Song& song)
{
	appendSong(song);
}

void MusicList::push_back(const QList<Song>& list)
{
	appendSong(list);
}

void MusicList::push_front(const Song& song)
{
	QMutexLocker<QMutex> locker(m_mutex);
	QStandardItem* item = new QStandardItem;
	item->setData(song.cover, Qt::UserRole + 1);
	item->setData(song.songName, Qt::UserRole + 2);
	item->setData(song.singerName, Qt::UserRole + 3);
	item->setData(song.duration_s, Qt::UserRole + 4);
	item->setData(song.url, Qt::UserRole + 5);
	item->setData(false, Qt::UserRole + 6);	//is Playing row
	emit push_frontFinished(item);

}

//void MusicList::switchList(ListType type)
//{
//	m_listView->setModel(m_models[type]);
//	m_curModel = m_models[type];
//}

QUrl MusicList::nextMusic()
{
	auto songCount = m_curModel->rowCount();
	m_curModel->itemFromIndex(m_curModel->index(m_playIndex, 0))
		->setData(false, Qt::UserRole + 6);
	switch (m_playMode)
	{
		//顺序
	case MusicList::PlayMode::PlayInOrder:
		//重复
	case MusicList::PlayMode::RepeatSingle:

		m_playIndex = (m_playIndex + 1) % songCount;
		break;

		//随机
	case MusicList::PlayMode::Shuffle:	
	{
		int i = m_playIndex;
		int count = 0;
		do
		{
			std::srand(std::time(nullptr));
			i = rand() % songCount;
		} while (i == m_playIndex && count < 3);
		m_playIndex = i;
	}
	break;

	
	default:
		break;
	}

	helightRow(m_playIndex);
	m_listView->setCurrentIndex(m_curModel->index(m_playIndex, 0));

	return 	m_curModel->data(m_curModel->index(m_playIndex, 0), Qt::UserRole + 5).toUrl();
}

QUrl MusicList::prevMusic()
{
	auto songCount = m_curModel->rowCount();
	m_curModel->itemFromIndex(m_curModel->index(m_playIndex, 0))
		->setData(false, Qt::UserRole + 6);
	switch (m_playMode)
	{
	case MusicList::PlayMode::PlayInOrder:
	case MusicList::PlayMode::RepeatSingle:
		if (m_playIndex == 0)
		{
			m_playIndex = songCount - 1;
		}
		else
		{
			m_playIndex = m_playIndex - 1;
		}
		break;
	case MusicList::PlayMode::Shuffle:
	{
		int i = m_playIndex;
		int count = 0;
		do
		{
			std::srand(std::time(nullptr));
			i = rand() % songCount;
		} while (i == m_playIndex && count < 3);
		m_playIndex = i;
	}
		break;


	default:
		break;
	}

	helightRow(m_playIndex);
	m_listView->setCurrentIndex(m_curModel->index(m_playIndex,0));
	return m_curModel->data(m_curModel->index(m_playIndex, 0), Qt::UserRole + 5).toUrl();
}

MusicList::PlayMode MusicList::playMode() const
{
	return m_playMode;
}

void MusicList::helightRow(int index)
{
	m_playIndex = index;
	m_curModel->itemFromIndex(m_curModel->index(index,0))
		->setData(true, Qt::UserRole + 6);
}


void MusicList::initAllConnect()
{
	connect(this, &MusicList::appendSongFinished,
		[this](QStandardItem* item) {
			m_curModel->appendRow(item);
			//playIndex == playIdex;
		});

	connect(this, &MusicList::push_frontFinished,
		[this](QStandardItem* item) {
			m_curModel->insertRow(0,item);
			++m_playIndex;
		});


	connect(m_listView, &MusicListView::mouseLeftDoubleClicked, this,
		[this](const QModelIndex& index) {

			//qDebug() << __FUNCTION__ << "url is " << index.data(Qt::UserRole + 5).toUrl();
				m_curModel->itemFromIndex(m_curModel->index(m_playIndex, 0))
					->setData(false, Qt::UserRole + 6);
				m_playIndex = index.row();
				qDebug() << "m_playIndex = " << m_playIndex;
				m_curModel->itemFromIndex(index)
					->setData(true, Qt::UserRole + 6);
				emit doubleClicked(index.data(Qt::UserRole + 5).toUrl());
		});
}

